package com.erp.app.report.genericInquiry;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.select.beans.SelectDelegate;
import com.erp.app.report.genericInquiry.inquiryCriteria.beans.vo.InquiryCriteriaVO;
import com.erp.app.report.genericInquiry.inquiryDisplay.beans.vo.InquiryDisplayVO;
import com.erp.app.report.genericInquiry.inquiryMaster.beans.GenericInquiryMasterVO;
import com.erp.app.report.genericInquiry.inquiryOutput.beans.vo.InquiryOutputVO;
import com.erp.framework.SimpleConstants;
import com.erp.framework.controller.form.SimpleSearchStyleForm;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.DateTimeUtil;
import com.erp.framework.util.logger.Logger;

public class GenericInquirySearchForm extends SimpleSearchStyleForm {
    private String inquiryType;
    private String filename;
    private GenericInquiryMasterVO genericInquiryMasterVO;

    private String[] srchFrom;
    private String[] srchFromLabel;
    private String[] srchTo;
    private String[] srchToLabel;
    
    private String[] srchDisableChkBox;
    private String[] srchFromTmp;
    private String[] srchToTmp;

    private Collection defaultValuesList;
    private Collection reportResultList;    
    
    boolean searched = false;
    boolean fileDownload = false;
    private String exceedMaxRslt;

	public String getExceedMaxRslt() {
		return exceedMaxRslt;
	}

	public void setExceedMaxRslt(String exceedMaxRslt) {
		this.exceedMaxRslt = exceedMaxRslt;
	}

	public void otherValidate(ActionErrors errors) {
		//User editingUser = (User) session.getAttribute(SimpleConstants.SESSION_USER);
        ChildList list = genericInquiryMasterVO.getChilds(InquiryCriteriaVO.class);
        Iterator it = list.iterator();
        InquiryCriteriaVO critVO = null;
        int counter = -1;
        String displayLabelFrom = "";
        String fieldNature = "";
        
        boolean isFromFieldEmpty = true;
        boolean isToFieldEmpty = true;
        
        int listSize = list.size();
        
        //Victor
        if (srchFrom == null)
        	return;
        if (srchTo == null)
        	return;

        try{
	        while (it.hasNext()) {
	        	counter++;
                
//                if(counter >= (listSize-2)){
//                    break;
//                }
                
	            critVO = (InquiryCriteriaVO)it.next();
	            fieldNature = CommonUtil.nullToString(critVO.getFieldNature());
	            
	            isFromFieldEmpty = CommonUtil.isEmpty(srchFrom[counter]);
	            isToFieldEmpty = true;
	            boolean isFromToField = false;
	            
	            if (critVO.getFieldType().equals(GenericInquiryConstants.FIELD_TYPE_TEXT_FM_TO)
	            		|| critVO.getFieldType().equals(GenericInquiryConstants.FIELD_TYPE_TEXT_FM_TO_OC)
	            		|| critVO.getFieldType().equals(GenericInquiryConstants.FIELD_TYPE_DROP_DOWN_FM_TO)) {
	                displayLabelFrom = " From";
	                isToFieldEmpty = CommonUtil.isEmpty(srchTo[counter]);
	                isFromToField = true;
	            }
	
	            if (GenericInquiryConstants.FLAG_SEARCH_CRITERIA_MANDATORY.equals(CommonUtil.nullToString(critVO.getMandatoryFlag()))
	            		|| GenericInquiryConstants.FLAG_SEARCH_CRITERIA_MANDATORY_WITH_BLANK.equals(critVO.getMandatoryFlag())) {
	            	if (isFromFieldEmpty) {
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.general.field.blank", critVO.getFieldName()));
	                }

		            if (isFromToField) {
		            	if (GenericInquiryConstants.FLAG_SEARCH_CRITERIA_MANDATORY.equals(CommonUtil.nullToString(critVO.getMandatoryFlag()))){           	    
			            	if (isToFieldEmpty) {
			                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.general.field.blank", critVO.getFieldName()));
			            	}
		            	}
		            }
	            }
	
	            if (isFromFieldEmpty && isToFieldEmpty)
	                continue;
	            
	            if (GenericInquiryConstants.DATA_TYPE_DATE.equals(critVO.getFieldNature())) {
	                if (!isFromFieldEmpty && !CommonUtil.isValidDate(srchFrom[counter])) {
	                	if (!CommonUtil.isValidDate(srchFrom[counter], ProjectConstants.INPUT_DATE_FORMAT_2)) {
					errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.date.format", critVO.getFieldName() + displayLabelFrom, GenericInquiryConstants.getDefaultDateFormat()));
				} else {
					srchFrom[counter] = CommonUtil.convertDateFormat(srchFrom[counter], ProjectConstants.INPUT_DATE_FORMAT_2, ProjectConstants.INPUT_DATE_FORMAT);
				}	                    
	                }
	
	                if (!CommonUtil.isEmpty(srchTo[counter]) && !CommonUtil.isValidDate(srchTo[counter])) {
	                    if (!CommonUtil.isValidDate(srchTo[counter], ProjectConstants.INPUT_DATE_FORMAT_2)) {
					errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.date.format", critVO.getFieldName() + " To", GenericInquiryConstants.getDefaultDateFormat()));
				} else {
					srchTo[counter] = CommonUtil.convertDateFormat(srchTo[counter], ProjectConstants.INPUT_DATE_FORMAT_2, ProjectConstants.INPUT_DATE_FORMAT);
				}
	                }
	                
	                if (!isFromFieldEmpty && !isToFieldEmpty 
	                        && CommonUtil.isValidDate(srchFrom[counter])
	                        && CommonUtil.isValidDate(srchTo[counter])
	                    	&& DateTimeUtil.compareDate(srchFrom[counter], srchTo[counter]) > 0) 
	        	    	errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.general.search_form_comparison", critVO.getFieldName() + " From", critVO.getFieldName() + " To"));{
	                }
	            } else if (GenericInquiryConstants.DATA_TYPE_NUMERIC.equals(fieldNature)) {
	                if (!isFromFieldEmpty && !CommonUtil.isValidNumber(srchFrom[counter])) {
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.number.format", critVO.getFieldName() + displayLabelFrom));
	                }
	
	                if (!isToFieldEmpty && !CommonUtil.isValidNumber(srchTo[counter])) {
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.number.format", critVO.getFieldName() + " To"));
	                }
	
	            } else if (GenericInquiryConstants.DATA_TYPE_POSITIVE_NUMERIC.equals(fieldNature)) {
	                if (!isFromFieldEmpty && !CommonUtil.isValidNumber(srchFrom[counter], 0, SimpleConstants.MIN_FLAG, true))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.positive_number.format", critVO.getFieldName() + displayLabelFrom));
	                
	                if (isToFieldEmpty && !CommonUtil.isValidNumber(srchTo[counter], 0, SimpleConstants.MIN_FLAG, true))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.positive_number.format", critVO.getFieldName() + " To"));
	                
	                if (!isFromFieldEmpty && !isToFieldEmpty 
	                        && CommonUtil.isValidNumber(srchFrom[counter], 0, SimpleConstants.MIN_FLAG, true) 
	                        && CommonUtil.isValidNumber(srchTo[counter], 0, SimpleConstants.MIN_FLAG, true)
	                        && srchTo[counter].compareTo(srchFrom[counter]) < 0)
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.from_greater_than_to", critVO.getFieldName()));
	                
	            } else if (GenericInquiryConstants.DATA_TYPE_NEGATIVE_NUMERIC.equals(fieldNature)) {
	                if (!isFromFieldEmpty && !CommonUtil.isValidNumber(srchFrom[counter], 0, SimpleConstants.MAX_FLAG, true))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.negative_number.format", critVO.getFieldName() + displayLabelFrom));
	                
	                if (isToFieldEmpty && !CommonUtil.isValidNumber(srchTo[counter], 0, SimpleConstants.MAX_FLAG, true))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.negative_number.format", critVO.getFieldName() + " To"));
	                
	                if (!isFromFieldEmpty && !isToFieldEmpty 
	                        && CommonUtil.isValidNumber(srchFrom[counter], 0, SimpleConstants.MAX_FLAG, true) 
	                        && CommonUtil.isValidNumber(srchTo[counter], 0, SimpleConstants.MAX_FLAG, true)
	                        && srchTo[counter].compareTo(srchFrom[counter]) < 0)
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.from_greater_than_to", critVO.getFieldName()));
	                
	            } else if (GenericInquiryConstants.DATA_TYPE_STRING.equals(fieldNature)) {
	                if (!isFromFieldEmpty && !isToFieldEmpty && srchTo[counter].compareTo(srchFrom[counter]) < 0)
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.from_greater_than_to", critVO.getFieldName()));
	                
	            } else if (GenericInquiryConstants.DATA_TYPE_INTEGER.equals(fieldNature)) {
	                if (!isFromFieldEmpty && !CommonUtil.isValidInteger(srchFrom[counter]))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.number.format", critVO.getFieldName() + displayLabelFrom));
	                
	                if (!isToFieldEmpty && !CommonUtil.isValidInteger(srchTo[counter]))
	
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.number.format", critVO.getFieldName() + " To"));
	                
	                if (!isFromFieldEmpty && !isToFieldEmpty 
	                        && CommonUtil.isValidInteger(srchFrom[counter]) 
	                        && CommonUtil.isValidInteger(srchTo[counter])
	                        && srchTo[counter].compareTo(srchFrom[counter]) < 0)
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.from_greater_than_to", critVO.getFieldName()));
	            } else if (GenericInquiryConstants.DATA_TYPE_POSITIVE_INTEGER.equals(fieldNature)) {
	                if (!isFromFieldEmpty && !CommonUtil.isValidInteger(srchFrom[counter], 0, SimpleConstants.MIN_FLAG, true))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.positive_integer.format", critVO.getFieldName() + displayLabelFrom));
	                
	                if (isToFieldEmpty && !CommonUtil.isValidInteger(srchTo[counter], 0, SimpleConstants.MIN_FLAG, true))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.positive_integer.format", critVO.getFieldName() + " To"));
	                
	                if (!isFromFieldEmpty && !isToFieldEmpty 
	                        && CommonUtil.isValidInteger(srchFrom[counter], 0, SimpleConstants.MIN_FLAG, true) 
	                        && CommonUtil.isValidInteger(srchTo[counter], 0, SimpleConstants.MIN_FLAG, true)
	                        && srchTo[counter].compareTo(srchFrom[counter]) < 0)
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.from_greater_than_to", critVO.getFieldName()));
	                
	            } else if (GenericInquiryConstants.DATA_TYPE_NEGATIVE_INTEGER.equals(fieldNature)) {
	                if (!isFromFieldEmpty && !CommonUtil.isValidInteger(srchFrom[counter], 0, SimpleConstants.MAX_FLAG, true))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.negative_integer.format", critVO.getFieldName() + displayLabelFrom));
	                
	                if (isToFieldEmpty && !CommonUtil.isValidInteger(srchTo[counter], 0, SimpleConstants.MAX_FLAG, true))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.negative_integer.format", critVO.getFieldName() + " To"));
	                
	                if (!isFromFieldEmpty && !isToFieldEmpty 
	                        && CommonUtil.isValidInteger(srchFrom[counter], 0, SimpleConstants.MAX_FLAG, true) 
	                        && CommonUtil.isValidInteger(srchTo[counter], 0, SimpleConstants.MAX_FLAG, true)
	                        && srchTo[counter].compareTo(srchFrom[counter]) < 0)
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.from_greater_than_to", critVO.getFieldName()));
	            } else if (GenericInquiryConstants.DATA_TYPE_YEAR.equals(fieldNature)) {
	                if (!isFromFieldEmpty && !CommonUtil.isValidYear(srchFrom[counter]))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.year.format", critVO.getFieldName() + displayLabelFrom, srchFrom[counter]));
	                
	                if (!isToFieldEmpty && !CommonUtil.isValidYear(srchTo[counter]))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.year.format", critVO.getFieldName() + " To", srchTo[counter]));
	                
	                if (!isFromFieldEmpty && !isToFieldEmpty
	                        && CommonUtil.isValidYear(srchFrom[counter]) 
	                        && CommonUtil.isValidYear(srchTo[counter])
	                        && Integer.parseInt(srchTo[counter]) < Integer.parseInt(srchFrom[counter]))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.from_greater_than_to", critVO.getFieldName()));                        
	            } else if (GenericInquiryConstants.DATA_TYPE_MONTH.equals(fieldNature)) {
	                if (!isFromFieldEmpty && !CommonUtil.isValidMonth(srchFrom[counter]))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.year.format", critVO.getFieldName() + displayLabelFrom, srchFrom[counter]));
	                
	                if (!isToFieldEmpty && !CommonUtil.isValidMonth(srchTo[counter]))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.year.format", critVO.getFieldName() + " To", srchTo[counter]));
	                
	                if (!isFromFieldEmpty && !isToFieldEmpty
	                        && CommonUtil.isValidMonth(srchFrom[counter]) 
	                        && CommonUtil.isValidMonth(srchTo[counter])
	                        && Integer.parseInt(srchTo[counter]) < Integer.parseInt(srchFrom[counter]))
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.from_greater_than_to", critVO.getFieldName()));                              
	            } else if (GenericInquiryConstants.DATA_TYPE_YEAR_MONTH.equals(fieldNature)) {
	/*                if (!isFromFieldEmpty && 
	                		(!CommonUtil.isValidDate(srchFrom[counter], ProjectConstants.YEAR_FORMAT + "/" + ProjectConstants.MTH_FORMAT)
								|| !CommonUtil.isValidDate(srchFrom[counter], ProjectConstants.MTH_FORMAT + "/" + ProjectConstants.YEAR_FORMAT)
	                			|| !CommonUtil.isValidDate(srchFrom[counter], ProjectConstants.YEAR_FORMAT + ProjectConstants.MTH_FORMAT)
	                			|| !CommonUtil.isValidDate(srchFrom[counter], ProjectConstants.MTH_FORMAT + ProjectConstants.YEAR_FORMAT)
								|| !CommonUtil.isValidDate(srchFrom[counter], ProjectConstants.SHORT_YEAR_FORMAT + "/" + ProjectConstants.MTH_FORMAT)
								|| !CommonUtil.isValidDate(srchFrom[counter], ProjectConstants.MTH_FORMAT + "/" + ProjectConstants.SHORT_YEAR_FORMAT)
								|| !CommonUtil.isValidDate(srchFrom[counter], ProjectConstants.SHORT_YEAR_FORMAT + ProjectConstants.MTH_FORMAT)
								|| !CommonUtil.isValidDate(srchFrom[counter], ProjectConstants.MTH_FORMAT + ProjectConstants.SHORT_YEAR_FORMAT)
	                		)
						) {*/
	                if (!isFromFieldEmpty && 
		        		( !CommonUtil.isValidDate(srchFrom[counter], ProjectConstants.MTH_FORMAT + "/" + ProjectConstants.YEAR_FORMAT))
	                ) {
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.monthyear.format", critVO.getFieldName() + displayLabelFrom, srchFrom[counter]));
	                }
	                
	                if (!isToFieldEmpty && 
	    	        		( !CommonUtil.isValidDate(srchTo[counter], ProjectConstants.MTH_FORMAT + "/" + ProjectConstants.YEAR_FORMAT))
					) {
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.monthyear.format", critVO.getFieldName() + " To", srchTo[counter]));
	                }
	                
	                if (!isFromFieldEmpty && !isToFieldEmpty
	                        && ( CommonUtil.isValidDate(srchFrom[counter], ProjectConstants.MTH_FORMAT + "/" + ProjectConstants.YEAR_FORMAT)) 
	                        && ( CommonUtil.isValidDate(srchTo[counter], ProjectConstants.MTH_FORMAT + "/" + ProjectConstants.YEAR_FORMAT))
	                        && DateTimeUtil.compareDate(srchFrom[counter], srchTo[counter], ProjectConstants.MTH_FORMAT + "/" + ProjectConstants.YEAR_FORMAT) > 0)
	                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.from_greater_than_to", critVO.getFieldName()));                              
	            }
	        }
        } catch (Exception e){
        	Logger.logExceptions(this, e);
        	errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("exception.general", e.getMessage()));
        }
    }

    // Get/Setters for String variables
    public String getInquiryType() {
        return inquiryType;
    }
    public void setInquiryType(String inquiryType) {
        this.inquiryType = inquiryType;
    }
    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    // Get/Setters for Other things
    public GenericInquiryMasterVO getGenericInquiryMasterVO() {
        return genericInquiryMasterVO;
    }
    public void setGenericInquiryMasterVO(GenericInquiryMasterVO vo) {
        this.genericInquiryMasterVO = vo;
    }

    // Get/Setters for String Arrays
    public String[] getSrchFrom() {
        return srchFrom;
    }
    public void setSrchFrom(String[] srchFrom) {
        this.srchFrom = srchFrom;
    }
    public void setSrchFrom(int index, String str) {
        this.srchFrom[index] = str;
    }
    public String[] getSrchTo() {
        return srchTo;
    }
    public void setSrchTo(String[] srchTo) {
        this.srchTo = srchTo;
    }
    public String[] getSrchFromLabel() {
        return srchFromLabel;
    }
    public void setSrchFromLabel(String[] srchFromLabel) {
        this.srchFromLabel = srchFromLabel;
    }
    public void setSrchFromLabel(int index, String str) {
        this.srchFromLabel[index] = str;
    }

    public Collection getReportResultList() {
        return reportResultList;
    }
    public void setReportResultList(Collection reportResultList) {
        this.reportResultList = reportResultList;
    }
    /** convert search criteria in the right format and return it to the calling method
     *
     * @return a copy of the parsed string arrays
     */
    public String[] parseSrchFrom() {
    	//Victor
    	if(srchFrom == null)
    		return null;
    	else
    		return parseInputValues(srchFrom);
    }

    /** convert search criteria in the right format and return it to the calling method
     *
     * @return a copy of the parsed string arrays
     */
    public String[] parseSrchTo() {
    	//Victor
    	if(srchFrom == null)
    		return null;
    	else
    		return parseInputValues(srchTo);
    }

    private String[] parseInputValues(String[] values) {
        Collection critList = genericInquiryMasterVO.getLocalChilds(InquiryCriteriaVO.class);

        if (critList == null) {
            System.err.println("GenericInquiry[parsing search form values] criteria list is null.  This should not happen!");
            return null;
        }

        String[] rslt = new String[critList.size()];
        int counter = 0;
        boolean firstToken = true;
        StringBuffer sb = null;
        InquiryCriteriaVO criteria = null;
        int listSize = values.length;

        for (Iterator it = critList.iterator(); it.hasNext();) {
            criteria = (InquiryCriteriaVO)it.next();
            firstToken = true;
            sb = new StringBuffer("");

            if (GenericInquiryConstants.FIELD_TYPE_MULTIPLE_DROP_DOWN.equals(criteria.getFieldType()) ||
            		GenericInquiryConstants.FIELD_TYPE_MULTIBOX.equals(criteria.getFieldType()) ||
            		GenericInquiryConstants.FIELD_TYPE_MULTIBOX_HORIZONTAL.equals(criteria.getFieldType())) {
                StringTokenizer tokens = new StringTokenizer(values[counter], GenericInquiryConstants.DELIMITER_MULTIPLE_DROP_DOWN);

                while (tokens.hasMoreTokens()) {
                    if (!firstToken)
                        sb.append(GenericInquiryConstants.DELIMITER_MULTIPLE_DROP_DOWN);
                    sb.append(tokens.nextToken());
                    firstToken = false;
                }
            } else {
                String temp = values[counter];

                // assuming all values are already validated, so we need to convert the input dates to the appropriate date format for stored proc
                // in this case, dd/mm/yyyy
                if (CommonUtil.nullToString(criteria.getFieldNature()).equals(GenericInquiryConstants.DATA_TYPE_DATE)) {
                    if (temp.indexOf('/') == -1) {
                        try {
	                        Date date = CommonUtil.stringToDate(temp, ProjectConstants.INPUT_DATE_FORMAT);
	                        temp = CommonUtil.nullToString(CommonUtil.dateToString(date));
                        //} catch (ParseException e) {
                            //Logger.logExceptions(e);
                        //}
                        } catch (Exception e) {
                        	Logger.logExceptions(this, e);
                        }
                    }
                }

                sb.append(temp);
            }
            rslt[counter] = sb.toString().trim();
            counter++;
            
//            if(counter >= (listSize-2)){
//                break;
//            }

        }

        return rslt;
    }

    public void resetForm() {
        super.resetForm();

        if (genericInquiryMasterVO != null)
            genericInquiryMasterVO.removeChilds(InquiryDisplayVO.class);

        populateDefaultValues(this.defaultValuesList);

        if (reportResultList != null)
            reportResultList.clear();
        
        if (this.pageControl != null) {
        	this.pageControl.release();
        	this.pageControl = null;
    	}
    }

    /*public String parseQueryString(HttpSession session, String query, int index) {
        ArrayList critList = genericInquiryMasterVO.getLocalChilds(InquiryCriteriaVO.class);
        InquiryCriteriaVO vo = (InquiryCriteriaVO)critList.get(index);

        if (CommonUtil.isEmpty(query))
            return "";

        StringBuffer rslt = new StringBuffer(query);
        int idx = 0;
        int eIdx = 0;

        while (idx != -1) {
            query = rslt.toString();
            idx = query.indexOf(GenericInquiryConstants.QUERYSTRING_PLACEHOLDER_COMPANY_CODE, eIdx);
            if (idx > -1) {
                eIdx = query.indexOf(GenericInquiryConstants.QUERYSTRING_SUFFIX, idx) + GenericInquiryConstants.QUERYSTRING_SUFFIX.length();
                String companyCode = "'" + SysParamMap.getInstance().getAppropriateCompanyCode(vo.getDdShare(), (String) session.getAttribute(ProjectConstants.SESSION_COMPANY_CODE)) + "'";
                rslt.replace(idx, eIdx, companyCode);
            }
        }

        idx = 0;
        eIdx = 0;

        while(idx != -1) {
            query = rslt.toString();
	        idx = query.indexOf(GenericInquiryConstants.QUERYSTRING_PLACEHOLDER_DEPT, eIdx);
	        if (idx > -1) {
	            eIdx = query.indexOf(GenericInquiryConstants.QUERYSTRING_SUFFIX, idx) + GenericInquiryConstants.QUERYSTRING_SUFFIX.length();
	            String depts = "'" + GenericInquiryUtil.getDataAccessSecurityWhereClause(session)+"'";
	            rslt.replace(idx, eIdx, depts);
	        }
        }

        idx = 0;
        eIdx = 0;

        while(idx != -1) {
            query = rslt.toString();
	        idx = query.indexOf(GenericInquiryConstants.QUERYSTRING_PLACEHOLDER_USER, eIdx);
	        if (idx > -1) {
	            eIdx = query.indexOf(GenericInquiryConstants.QUERYSTRING_SUFFIX, idx) + GenericInquiryConstants.QUERYSTRING_SUFFIX.length();
	            User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
	            rslt.replace(idx, eIdx, "'" + editingUser.getUserCode()+"'");
	        }
        }

        idx = 0;
        eIdx = 0;

        while(idx != -1) {
            query = rslt.toString();
	        idx = query.indexOf(GenericInquiryConstants.QUERYSTRING_PLACEHOLDER_CRITERIA_FIELD, eIdx);
	        if (idx > -1) {
	            eIdx = query.indexOf(GenericInquiryConstants.QUERYSTRING_SUFFIX, idx) + GenericInquiryConstants.QUERYSTRING_SUFFIX.length();

	            int field_num;

	            try {
	                field_num = Integer.parseInt(rslt.substring(idx + GenericInquiryConstants.QUERYSTRING_PLACEHOLDER_CRITERIA_FIELD.length(), eIdx - GenericInquiryConstants.QUERYSTRING_SUFFIX.length()));
	                rslt.replace(idx, eIdx, srchFrom == null ? "''" : "'" + srchFrom[field_num-1] + "'");
	            } catch (NumberFormatException e) {
	                    Logger.debug(this, "GenericInquiryUtil:Invalid CFIELD Index! Check your "
	                            + GenericInquiryConstants.QUERYSTRING_PREFIX
	                            + "CFIELD(line_no)"
	                            + GenericInquiryConstants.QUERYSTRING_SUFFIX
	                            + " in your DD_WHERE.\n" + e);
	            }
	        }
        }

        return rslt.toString();
    }*/

    public void setDefaultValues(Collection defaultValuesList) {
        if (defaultValuesList == null || defaultValuesList.size() != 2)
            return;

        this.defaultValuesList = defaultValuesList;
        populateDefaultValues(defaultValuesList);
    }

    private void populateDefaultValues(Collection list) {
        if (list == null)
            return;

        Iterator it = defaultValuesList.iterator();
        int size = genericInquiryMasterVO.getLocalChilds(InquiryCriteriaVO.class).size();

        srchFrom = (String[]) ((Collection)it.next()).toArray(new String[size]);
        srchTo = (String[]) ((Collection)it.next()).toArray(new String[size]);
        srchFromLabel = new String[size];
        srchToLabel = new String[size];
    }

    public boolean isFirstPage() {
        int page = getCurrentPage();

        return page == 1;
    }

    public boolean isLastPage() {
        int page = getCurrentPage();

        return page == getTotalPage();
    }

	public String[] getSrchToLabel() {
		return srchToLabel;
	}
	public void setSrchToLabel(String[] srchToLabel) {
		this.srchToLabel = srchToLabel;
	}

	public String[] getSrchDisableChkBox() {
		return srchDisableChkBox;
	}
	public void setSrchDisableChkBox(String[] srchDisableChkBox) {
		this.srchDisableChkBox = srchDisableChkBox;
	}

	public String[] getSrchFromTmp() {
		return srchFromTmp;
	}
	public void setSrchFromTmp(String[] srchFromTmp) {
		this.srchFromTmp = srchFromTmp;
	}
	public String[] getSrchToTmp() {
		return srchToTmp;
	}
	public void setSrchToTmp(String[] srchToTmp) {
		this.srchToTmp = srchToTmp;
	}
	
	public String getTableWidth() {
        return getTableWidth(false);
    }
    
    public String getTableWidth(boolean excludeHidden) {
        int widths = 0;
        
        if (genericInquiryMasterVO == null)
        	return "0";
        
        Collection list = genericInquiryMasterVO.getLocalChilds(InquiryDisplayVO.class);
        
        if (list == null)
            return "0";
        
        Iterator it = list.iterator();
        InquiryDisplayVO vo = null;
        
        while (it.hasNext()) {
           vo = (InquiryDisplayVO)it.next();
           
           if (excludeHidden && CommonUtil.nullToString(vo.getHiddenFlag()).equals(GenericInquiryConstants.FLAG_HIDDEN))
               continue;
           
           if (vo.getColumnWidth() == null)
        	   return "";
           
           widths += vo.getColumnWidth().intValue();
        }
        
        return String.valueOf(widths);
    }
    
    public boolean isSearched () {
        Collection list = genericInquiryMasterVO.getLocalChilds(InquiryDisplayVO.class);
        
        return list != null && list.size() > 0;
    }
    
    public boolean isFileDownload() {
        return this.reportResultList != null;
    }
   //just for html excel
    public void validateRowCount(GenericInquirySearchForm form){
    	ArrayList reportList = (ArrayList)form.getReportResultList();
    	InquiryOutputVO vo = (InquiryOutputVO)reportList.get(0);
    	SelectDelegate sd = new SelectDelegate();
    	String sql= "SELECT NVL(COUNT (*),0) "
				  + "	  FROM T_INQUIRY_OUTPUT "
				  + "	 WHERE COMPANY_CODE='"+vo.getCompanyCode()+"' "
				  + "	   AND RPT_TIME = '"+vo.getRptTime()+"'"
				  + "	   AND INQUIRY_TYPE = '"+vo.getInquiryType()+"'"
				  + " 	   AND UPPER (FIELD_01) LIKE '%<TR%'";
    	String count = CommonUtil.nullToString(sd.selectOneValue(sql));
    	if (Integer.parseInt(count) > ProjectConstants.HTML_TXT_ROW_NUM) {
			form.setExceedMaxRslt(ProjectConstants.GENERIC_YES);
		}else{
			form.setExceedMaxRslt(null);
		}
    }
}
